package com.ewhale.feitengguest.ui.user.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.presenter.user.mine.ResetPasswordPresenter;
import com.ewhale.feitengguest.utils.Contants;
import com.ewhale.feitengguest.view.user.mine.ResetPasswordView;
import com.orhanobut.hawk.Hawk;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.CheckUtil;
import com.simga.library.utils.HawkKey;
import com.simga.library.widget.BGButton;

import androidx.core.content.ContextCompat;

import butterknife.BindView;
import butterknife.OnClick;

public class ResetPasswordActivity extends MBaseActivity<ResetPasswordPresenter> implements ResetPasswordView, TextWatcher {
    @BindView(R.id.et_old_password)
    EditText mEtOldPassword;
    @BindView(R.id.et_new_password1)
    EditText mEtNewPassword1;
    @BindView(R.id.iv_eye1)
    ImageView mIvEye1;
    @BindView(R.id.et_password_again)
    EditText mEtPasswordAgain;
    @BindView(R.id.iv_eye2)
    ImageView mIvEye2;
    @BindView(R.id.btn_commit)
    BGButton mBtnComfir;

    private String token;
    private boolean isPwdShow;
    private boolean isPwd2Show;

    public static void open(MBaseActivity context, String token) {
        Bundle bundle = new Bundle();
        bundle.putString("token", token);
        context.startActivity(bundle, ResetPasswordActivity.class);
    }

    @Override
    protected void onGetBundle(Bundle bundle) {
        token = bundle.getString("token");
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_reset_password;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setTitle("修改登录密码");
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return true;
    }

    @Override
    protected void initListener() {
        mEtOldPassword.addTextChangedListener(this);
        mEtNewPassword1.addTextChangedListener(this);
        mEtPasswordAgain.addTextChangedListener(this);
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

    @OnClick({R.id.iv_eye1, R.id.iv_eye2, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_eye1:
                if (isPwdShow) {
                    mEtNewPassword1.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mIvEye1.setImageResource(R.mipmap.a_icon_eyes_n);
                } else {
                    mEtNewPassword1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mIvEye1.setImageResource(R.mipmap.a_icon_eyes_s);
                }
                isPwdShow = !isPwdShow;
                break;
            case R.id.iv_eye2:
                if (isPwd2Show) {
                    mEtPasswordAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mIvEye2.setImageResource(R.mipmap.a_icon_eyes_n);
                } else {
                    mEtPasswordAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mIvEye2.setImageResource(R.mipmap.a_icon_eyes_s);
                }
                isPwd2Show = !isPwd2Show;
                break;
            case R.id.btn_commit:
                String oldPwd = mEtOldPassword.getText().toString();
                String newPwd1 = mEtNewPassword1.getText().toString();
                String newPwd2 = mEtPasswordAgain.getText().toString();
                presenter.resetPassword(Hawk.get(HawkKey.NOW_APP_STATUS, Contants.USER_STATUS).equals(Contants.SHOP_STATUS) ? 1 : 0, oldPwd, newPwd1, newPwd2, token);
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (CheckUtil.isNull(mEtOldPassword.getText().toString()) ||
                CheckUtil.isNull(mEtNewPassword1.getText().toString()) ||
                CheckUtil.isNull(mEtPasswordAgain.getText().toString())) {
            mBtnComfir.setNormalSolid(Color.parseColor("#EEEEEE"));
            mBtnComfir.setTextColor(Color.parseColor("#BBBBC2"));
            mBtnComfir.setNormalStroke(1, Color.parseColor("#D4D4D4"));
            mBtnComfir.setEnabled(false);
        } else {
            mBtnComfir.setNormalSolid(ContextCompat.getColor(mContext, R.color.main_color));
            mBtnComfir.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            mBtnComfir.setNormalStroke(1, ContextCompat.getColor(mContext, R.color.main_color));
            mBtnComfir.setEnabled(true);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void resetSuccess() {
        showToast("密码修改成功，请使用新密码登录");
        sendBroadcast(new Intent(HawkKey.USER_LOGIN_OUT));
    }
}
